//
//  DateUtility.m
//  TestAfn2
//
//  Created by xujingjing on 16/7/8.
//  Copyright © 2016年 xujingjing. All rights reserved.
//


#import "DateUtility.h"

@implementation DateUtility

/**
 *  字符串转为NSDate
 *
 *  @param dateString 字符串
 *
 *  @return NSDate时间
 */
+ (NSDate *)dateFromString:(NSString *)dateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *destDate= [dateFormatter dateFromString:dateString];
 
    return destDate;
    
}


/**
 *  NSDate转为NSString
 *
 *  @param date NSDate
 *
 *  @return NSString
 */
+(NSString *)stringFromDate:(NSDate *)date{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
 
    return destDateString;
    
}


/**
 *  时间转时间戳
 *
 *  @param string 时间字符串
 *
 *  @return 时间戳字符串
 */
+(NSString *)timeInterverStringFromTimeString:(NSString *)string{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
  
    NSDate* date = [formatter dateFromString:string];
   
    return [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
}



/**
 *  时间戳转时间
 *
 *  @param timeIntervel 时间戳字符串
 *
 *  @return 时间 2016-07-06
 */
+(NSString *)stringFromTimeInterVerString:(NSString *)timeIntervel{
    
    NSDate *confromTimesp =  [NSDate dateWithTimeIntervalSince1970:[timeIntervel floatValue]];
  
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
   
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    return [formatter stringFromDate:confromTimesp];
}

/**
 *  时间戳转时间 含有时分秒的
 *
 *  @param timeIntervel 时间戳字符串
 *
 *  @return 2016-07-21 11:23:00
 */
+(NSString *)stringWithHHMMSSFromTimeInterVerString:(NSString *)timeIntervel{

    NSTimeInterval time=[timeIntervel doubleValue];
 
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
  
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   
    return [dateFormatter stringFromDate: detaildate];
}


/**
 *  转为UTC时间
 *
 *  @param sourceDate 待转化的时间
 *
 *  @return 转换后的时间
 */
+ (NSDate*) convertToUTC:(NSDate*)sourceDate
{
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval gmtInterval =  currentGMTOffset - gmtOffset;
    NSDate* destinationDate = [NSDate dateWithTimeInterval:gmtInterval sinceDate:sourceDate];
    return destinationDate;
}



/**
 *  时间字符串：1999-09-09 00:00:00
 *
 *  @param time 时间戳
 *
 *  @return 时间字符串
 */
+(NSString *)convertedLongToStringFormat:(double )time
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *dateText = [DateUtility stringFromDate:date];
    return dateText;
}

/**
 *  最近打卡时间 1900-00-00 12-00
 *
 *  @param time
 *
 *  @return
 */
+(NSString *)trimLocktimeFormat:(double)time{

    NSString *date = [self convertedLongToStringFormat:time];
    return [date substringToIndex:16];
}

/**
 *  计算指定时间与当前时间的差
 *
 *  @param created 指定时间
 *
 *  @return 时间差
 */
+(NSString *)convertedTimeDifferenceFormat:(double)time{
    if (time == 0){
        return nil;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *created = [DateUtility convertedLongToStringFormat:time];
    
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval s = now - time;//秒
    int m = s  / 60;//分钟
    if (m == 0) {
        return @"刚刚";
    }
    int h = m / 60; // 小时
    if (h == 0) {
        return [NSString stringWithFormat:@"%d分钟前",m ];
    }
    int d = h / 24;//天
    if (d == 0) {
        return [NSString stringWithFormat:@"%d小时前",h ];
    }
    if (d <= 30) {
        return [NSString stringWithFormat:@"%d天前",d ];
    }
    NSInteger year = [DateUtility year:date];
    NSInteger thisYear = [DateUtility year:[NSDate date]];
    if (year == thisYear) {
        return [created substringWithRange:NSMakeRange(5, 5)];
    }
    return @"1年前";
    
}

/**
 *  根据日期获得年份
 *
 *  @param date 时间
 *
 *  @return 年份
 */
+(NSInteger)year:(NSDate *)date{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date]; // Get necessary date components
    return  [components year];
}

/**
 *  根据日期获得月份
 *
 *  @param date 时间
 *
 *  @return 月份
 */
+(NSInteger)month:(NSDate *)date{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date]; // Get necessary date components
    return  [components month];
}
/**
 *  根据日期获得
 *
 *  @param date 时间
 *
 *  @return 日期
 */
+(NSInteger)day:(NSDate *)date{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date]; // Get necessary date components
    return  [components day];
}

/**
 *  截取时间: 若 00:00
 *
 *  @param created 时间
 *
 *  @return 截取后的时间
 */
+(NSString *)trimTimeFormat:(NSString *)created{
    if ([created isEqualToString:@"(null)"]) {
        return nil;
    }
    NSDate *date = [NSDate date];
    NSString *thisday= [DateUtility stringFromDate:date];
    //当前年份
    NSString * thisYear =   [NSString stringWithFormat:@"%ld",[DateUtility year:date]];
    if ([created hasPrefix:thisYear]) {
        //同一年
        NSString *sameday = [thisday   substringWithRange:NSMakeRange(0, 10)];
        if ([created hasPrefix:sameday]) {
            // 同一天 返回时间
            return [created substringWithRange:NSMakeRange(11, 5)];
        }
        return [created substringWithRange:NSMakeRange(5, 11)];
    }
    if (created.length >= 16) {
        return [created substringWithRange:NSMakeRange(0, 16)];
    }
    return created;
}

/**
 *  项目时间 11/24
 *
 *  @param time 时间戳
 *
 *  @return 截取后的时间
 */
+(NSString *)trimOnlyDateFormat:(NSTimeInterval)time
{
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSString * month =   [NSString stringWithFormat:@"%.2ld",(long)[DateUtility month:date]];
    NSString *day = [NSString stringWithFormat:@"%.2ld",(long)[DateUtility day:date]];
    day = [day stringByAppendingString:@"/"];
    day = [day stringByAppendingString:month];
    return day;
}

/**
 *  时间字符串：1999-09-09
 *
 *  @param time 时间戳
 *
 *  @return 时间字符串
 */
+(NSString *)trimDateToStringFormat:(double )time
{

    NSString *trimTime = [self convertedLongToStringFormat:time];
    
    return [trimTime substringToIndex:10];
}

//ZH
+(NSString *)trimLockZHtimeFormat:(double)ZHtime{
    
    NSString *date = [self convertedLongToStringFormat:ZHtime];
    return [date substringFromIndex:11];
}
@end
